
this_verion = '比赛专用版'

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from configparser import ConfigParser
import webbrowser
import Get_File
import os

config = ConfigParser()
config.read("config.ini",encoding="gbk")

def set_windows_x_y(window, window_width, window_height):
    # 设置窗口的宽度和高度
    window_width = window_width
    window_height = window_height

    screen_width = window.winfo_screenwidth()
    screen_height = window.winfo_screenheight()

    # 计算窗口的左上角位置，使其居中显示
    x_position = (screen_width - window_width) / 2
    y_position = (screen_height - window_height) / 2
    # 设置窗口的位置和大小
    window.wm_geometry(f"{str(window_width)}x{str(window_height)}+{str(int(x_position))}+{str(int(y_position))}")


def options_window():
    def get_file_path():
        path = filedialog.askopenfile()
        options_w.wm_attributes('-topmost', 'true')
        options_w.wm_attributes('-topmost', 'false')
        config.set('options','file_path',path.name)
        with open("config.ini","w") as f:
            config.write(f)
            f.close()

        entry_1.insert('0.0', ' '*50)
        entry_1.insert('0.0',config['options']['file_path'].split("/")[-1])

        options_w.update()
        options_w.wm_attributes('-topmost', 'true')
        options_w.wm_attributes('-topmost', 'false')

        print(path)
        print(path.name)
        return path

    def checkbutton_1_touch(var,section,options):
        config.set(section,options,str(var.get()))
        with open("config.ini",'w') as f:
            config.write(f)
            f.close()
            print("done",var.get())
        var.set(str(config[section][options]))
        print(config[section][options])
        options_w.update()

    options_w = Tk()
    set_windows_x_y(options_w,400,480)
    options_w.title("选项")
    options_w.iconbitmap('ico.ico')

    text_1 = Label(options_w,text="选项",font=("等线",38,'bold'))
    text_1.place(x=20,y=15)

    text_2 = Label(options_w,text="名字字典名字:",font=("等线",15))
    text_2.place(x=26,y=104)

    text_3 = Label(options_w,text="其他选项:",font=("等线",15))
    text_3.place(x=26,y=180)

    # 记录选项框
    checkbutton_1_var = BooleanVar()
    checkbutton_1_var.set(eval(config['options']['jilu']))
    # print(checkbutton_1_var.get())
    checkbutton_1 = Checkbutton(options_w,text="记录",font=("等线",12),variable=checkbutton_1_var,command=lambda:checkbutton_1_touch(checkbutton_1_var,"options","jilu"))
    checkbutton_1.place(x=50,y=207)

    # ,state = "disabled"
    entry_1 = Text(options_w,width=35,height=1,font=("黑体",12))
    entry_1.insert('insert', config['options']['file_path'].split("/")[-1])
    entry_1.place(x=26,y=129)

    button_1 = Button(options_w,text="···",font=("等线"),command=lambda:get_file_path())
    button_1.place(x=296,y=123)

    options_w.mainloop()

def abouts_window():
    def open_Web(url):
        webbrowser.open(url)
    def Check_for_updates():
        cache = Get_File.IniFileCache(
            'https://gitee.com/binzhijie/web/raw/master/rollcall_web/%20web_config_bs.ini',
            'web_config.ini')
        cache.save_to_cache()

        web_config = ConfigParser()
        web_config.read('web_config.ini', encoding='gbk')

        version = web_config['date']['version']
        if version > this_verion:
            messagebox.showinfo("更新","发现新版本！")
            text_4.insert('0.0', ' \n' * 100)
            print(web_config['updata']['content'],type(web_config['updata']['content']))
            text_4.insert('0.0', web_config['updata']['content'])

            return True
        elif version == this_verion:
            messagebox.showinfo('检查更新', f'当前已是最新版本！')
            text_4.insert('0.0', ' \n' * 100)
            print(web_config['updata']['content'], type(web_config['updata']['content']))
            text_4.insert('0.0', web_config['updata']['content'])
            return False

    def download_data():
        try:
            web_config = ConfigParser()
            web_config.read('web_config.ini', encoding='gbk')

            cache = Get_File.IniFileCache(str(web_config['updata']['download']),
                                          web_config['updata']['file_name'])
            cache.save_to_cache()

            os.system(f"start {web_config['updata']['file_name']}")
            os._exit(0)
        except:
            messagebox.showerror("错误","下载失败!")

    abouts_w = Tk()
    abouts_w.title("关于")
    abouts_w.iconbitmap("ico.ico")
    set_windows_x_y(abouts_w, 600, 400)

    text_1 = Label(abouts_w,text="RollCall 点名器", font=("等线",26,"bold"))
    text_1.place(x=14,y=15)

    text_2 = Label(abouts_w,text="软件为开源软件，如需使用源代码，\n请遵守本软件的开源许可证",font=("等线",12))
    text_2.place(x=14,y=72)

    text_3 = Label(abouts_w,text="软件更新",font=("等线",26,'bold'))
    text_3.place(x=325,y=15)

    text_4 = Text(abouts_w,width=18,height=9,font=("等线",20))
    text_4.place(x=325,y=67)

    button_1 = Button(abouts_w,text="检查更新",font=("等线",15),command=lambda:Check_for_updates())
    button_1.place(x=325,y=346)

    button_2 = Button(abouts_w,text="下载更新",font=("等线",15),command=lambda:download_data())
    button_2.place(x=462,y=346)

    abouts_w.mainloop()

if __name__ == "__main__":
    # options_window()
    abouts_window()